﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PracticeAzure.Website.Shared;
using PracticeAzure.Website.Data;
using PracticeAzure.Website.Models;

namespace PracticeAzure.Website.Controllers
{
    public class HomeController : PracticeAzureBaseController
    {
        [HttpGet]
        public ActionResult Index()
        {
            // tried to retrieve the recent announcements from cache
            var announcements = CacheFactory.Current.Get<IEnumerable<Announcement>>("announcements");
            // retrieve the announcements from database if full from the cache
            if (announcements == null)
            {
                announcements = Announcement.Recent()
                    .OrderByDescending(a => a.CreatedOn)
                    .ToList();
                CacheFactory.Current.Add(
                    "announcements", 
                    announcements, 
                    TimeSpan.FromSeconds(GlobalConfiguration.CacheExpirationInSeconds));
            }
            
            var model = new HomeIndexViewModel()
            {
                Announcements = announcements
            };
            return View(model);
        }

        [HttpGet]
        public ActionResult Author()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Recommendations()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Preface()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Tables()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Chapters()
        {
            var chapters = new Dictionary<string, string>();
            var container = GlobalConfiguration.ChaptersContainerName;
            chapters.Add(
                GlobalConfiguration.GetBlobAbsoluteUrl(container, "practice.windows.azure.chapters.2.1.pdf"),
                "2.1   Windows Azure 平台的组成部分和主要功能");
            chapters.Add(
                GlobalConfiguration.GetBlobAbsoluteUrl(container, "practice.windows.azure.chapters.3.3.2.pdf"),
                "3.3.2   Role 及其生命周期模型");
            chapters.Add(
                GlobalConfiguration.GetBlobAbsoluteUrl(container, "practice.windows.azure.chapters.4.2.1.pdf"),
                "4.2   使用 Table Service");
            chapters.Add(
                GlobalConfiguration.GetBlobAbsoluteUrl(container, "practice.windows.azure.chapters.6.3.pdf"),
                "6.3   SQL Azure 服务器端架构");
            chapters.Add(
                GlobalConfiguration.GetBlobAbsoluteUrl(container, "practice.windows.azure.chapters.7.1.3.pdf"),
                "7.1.3   使用 Eventing 模式");
            chapters.Add(
                GlobalConfiguration.GetBlobAbsoluteUrl(container, "practice.windows.azure.chapters.8.4.pdf"),
                "8.4  Web Role 的 Full IIS 特性");

            ViewBag.Chapters = chapters;
            return View();
        }

        [HttpGet]
        public ActionResult Samples()
        {
            var model = new HomeSamplesViewModel();
            var container = GlobalConfiguration.SamplesContainerName;
            // set the all samples blob
            model.SamplesAll = new HomeSamplesViewModel.SampleItem(
                "practicewindowsazure_samples_all.zip",
                "完整样例代码");
            // set the samples
            
            return View(model);
        }

        [HttpGet]
        public ActionResult Purchase()
        {
            return View();
        }
    }
}
